package com.wxx.service.bg;

import com.wxx.mapper.DicRoleAclMapper;
import com.wxx.mapper.DicRoleMapper;
import com.wxx.mapper.DicUserMapper;
import com.wxx.model.DicRole;
import com.wxx.model.DicRoleAcl;
import com.wxx.model.DicUser;
import com.wxx.util.AppUtil;
import com.wxx.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DicRoleServiceImpl implements DicRoleService {

    @Autowired
    private DicRoleMapper dicRoleMapper;
    @Autowired
    private DicUserMapper dicUserMapper;
    @Autowired
    private DicRoleAclMapper dicRoleAclMapper;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public Map del(String roleId) {
        //判断该角色是否被使用
        DicUser model = new DicUser();
        model.setRoleId(roleId);
        int flag = dicUserMapper.selectCount(model);
        if (flag > 0){
            return JsonData.returnMap(0,"该角色已被使用，不能删除");
        }

        //1.删除 角色与权限关联表
        DicRoleAcl roleAcl = new DicRoleAcl();
        roleAcl.setRoleId(roleId);
        dicRoleAclMapper.delete(roleAcl);
        //2.删除角色
        int i = dicRoleMapper.deleteByPrimaryKey(roleId);
        return JsonData.returnMap(i);
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public Map edit(DicRole dicRole) {
        //查询角色名是否存在,存在返回错误
        if (roleNameIsExist(dicRole)){
            return JsonData.returnMap(0, "角色名已存在");
        }
        dicRole.setUpdateTime(new Date());
        int i = dicRoleMapper.updateByPrimaryKeySelective(dicRole);
        return JsonData.returnMap(i);
    }

    @Transactional(propagation= Propagation.SUPPORTS)
    @Override
    public DicRole goEdit(String roleId) {
        return dicRoleMapper.selectByPrimaryKey(roleId);
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public Map add(DicRole dicRole) {

        //查询角色名是否存在,存在返回错误
        if (roleNameIsExist(dicRole)){
            return JsonData.returnMap(0, "角色名已存在");
        }

        dicRole.setRoleId(AppUtil.uuid());
        dicRole.setCreateTime(new Date());
        int i = dicRoleMapper.insertSelective(dicRole);
        return JsonData.returnMap(i);
    }

    private boolean roleNameIsExist(DicRole dicRole) {
        DicRole model = new DicRole();
        model.setRoleName(dicRole.getRoleName());
        int flag = dicRoleMapper.selectCount(model);
        if (flag > 0){
            return true;
        }
        return false;
    }

    @Transactional(propagation= Propagation.SUPPORTS)
    @Override
    public List<DicRole> findDicRoleListByExcludeAdmin() {
        List<DicRole> dicRoleList = dicRoleMapper.findDicRoleListByExcludeAdmin();
        return dicRoleList;
    }

    @Transactional(propagation= Propagation.SUPPORTS)
    @Override
    public List<DicRole> findDicRoleList() {
        return dicRoleMapper.findDicRoleList();
    }

}
